package cn.rongdao.jrkabao.ui.main.mainfragment.vip;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.statistic.alipay.PayManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.deposit.AccountInfoBean;
import cn.rongdao.jrkabao.bean.main.vip.MemberBean;
import cn.rongdao.jrkabao.bean.main.vip.VipBean;
import cn.rongdao.jrkabao.model.main.vip.MemberModel;
import cn.rongdao.jrkabao.model.main.vip.VipModel;
import cn.rongdao.jrkabao.ui.main.mainfragment.adapter.VipAdapter;
import cn.rongdao.jrkabao.ui.main.mainfragment.adapter.VipViewHolder;
import cn.rongdao.jrkabao.ui.main.mainfragment.uicomponent.MemberPowerUIComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.uicomponent.PersonUserInfoUIComponent;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.LoginUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;

/**
 * Created by jack on 2018/1/15
 */

public class MemberActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<VipViewHolder> {

    public static final String IS_FROM_MY_MEMBER = "is_from_my_member";

    public static void start(Context context) {
        FRStartActivity.start(context, MemberActivity.class);
    }

    public static void startForResult(Activity activity, boolean isFromMyMember) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(IS_FROM_MY_MEMBER, isFromMyMember);
        FRStartActivity.startForResult(activity, MemberActivity.class, bundle, MyMemberActivity.MEMBER_REQUEST_CODE);
    }

    @BindView(R.id.amem_user_info)
    PersonUserInfoUIComponent personUserInfoUIComponent;
    @BindView(R.id.amem_vip_power)
    MemberPowerUIComponent memberPowerUIComponent;
    @BindView(R.id.amem_tv_money)
    TextView tvMoney;
    @BindView(R.id.amem_tv_pay)
    TextView tvPay;
    @BindView(R.id.amem_vip_recyclerview)
    RecyclerView vipRecyclerView;
    private VipAdapter adapter;
    private String vipId;

    private boolean isFromMyMember;
    private AccountInfoBean accountInfoBean;

    @Override
    protected int layoutRes() {
        return R.layout.activity_member;
    }

    @Override
    protected void initView() {
        super.initView();
        BuriedUtil.kamao_yemian_jiazaiwancheng_apply(thisActivity(), 6);
        isFromMyMember = getIntent().getBooleanExtra(IS_FROM_MY_MEMBER, false);
        personUserInfoUIComponent.setPersonal(false);
        adapter = new VipAdapter(this, this);
        vipRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        vipRecyclerView.setAdapter(adapter);
        initData();
        initListener();
    }

    private void initListener() {
        tvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BuriedUtil.kamao_huiyuan_anniu_apply(thisActivity(), 3);
                if (LoginUtil.isLogin(thisActivity())) {
                    if (!CommonUtil.isMobileInstall(CommonUtil.A_LI_PAY_PACKAGE)) {
                        FRToast.showToastSafe("您还没有安装支付宝");
                        return;
                    }
                    getAliPayInfo();
                }
            }
        });
    }

    private void getAliPayInfo() {
        VipModel.getInstance().execute(vipId, new FRBaseObserver<VipBean>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(VipBean data) {
                goAliPay(data.getOrder_info());
            }
        });
    }

    private void goAliPay(String orderInfo) {
        PayManager payManager = new PayManager(thisActivity());
        payManager.setNeedH5(true);
        payManager.goToAliPay(orderInfo);
        payManager.setOnAliPaySuccess(new PayManager.OnAliPaySuccess() {
            @Override
            public void paySuccess() {
                initData();
                EventBus.getDefault().post(new MemberPaySuccessEvent());
                FRToast.showToastSafe("支付成功");
            }

            @Override
            public void payFailure() {
                FRToast.showToastSafe("支付失败，请重试");
            }
        });
    }

    private void initData() {
        personUserInfoUIComponent.getUserInfo(this);
        MemberModel.getInstance().execute(new FRBaseObserver<MemberBean>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(MemberBean data) {
                if (null != data.getVip_config()) {
                    adapter.setDataList(data.getVip_config());
                    if (data.getVip_config().size() > 0) {
                        adapter.initSelect();
                        vipId = data.getVip_config().get(0).getId();
                        setMoney(data.getVip_config().get(0).getPrice());
                    }
                }
                if (null != data.getVip_privilege()) {
                    memberPowerUIComponent.setContent(data.getVip_privilege());
                }
                memberPowerUIComponent.setLinkUrl(data.getVip_service_agreement());
            }
        });
    }

    private void setMoney(String money) {
        if (FRString.isEmpty(money)) {
            money = "0.00";
        }
        String content = FRCollection.appendAsString("总计￥", money, "");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(thisActivity(), R.color.cff7048)), 2, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvMoney.setText(ss);
    }

    public void finishActivity() {
        if (isFromMyMember) {
            setResult(RESULT_OK, new Intent().putExtra(MyMemberActivity.ACCOUNT_BEAN, accountInfoBean));
        }
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onItemClick(VipViewHolder vipViewHolder, int position) {
        if (!FRString.isEmpty(adapter.getDataList().get(position).getDay())) {
            buriedPoint(adapter.getDataList().get(position).getDay());
        }
        if (adapter.isSelect(position)) {
            return;
        }
        vipId = adapter.getDataList().get(position).getId();
        setMoney(adapter.getDataList().get(position).getPrice());
        adapter.setItemSelect(position);
    }

    private void buriedPoint(String day) {
        switch (day) {
            case "30":
                BuriedUtil.kamao_huiyuan_anniu_apply(thisActivity(), 0);
                break;
            case "90":
                BuriedUtil.kamao_huiyuan_anniu_apply(thisActivity(), 1);
                break;
            case "365":
                BuriedUtil.kamao_huiyuan_anniu_apply(thisActivity(), 2);
                break;
        }
    }

    public void setAccountBean(AccountInfoBean data) {
        accountInfoBean = data;
    }
}
